package com.iot.inter.controller.newinterface.trafficusage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.controller.newinterface.query.QueryPartnerByDeviceIdController;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.response.subResponse.QuerDeviceDayFlowResponse;
import com.iot.inter.entity.newpojo.response.subResponse.common.FlowByDay;
import com.iot.inter.entity.newrequest.trafficusage.appointdeviceday.request.RequestAppointDeviceDay;
import com.iot.inter.entity.newrequest.trafficusage.appointdeviceday.response.Flow_by_day;
import com.iot.inter.entity.newrequest.trafficusage.appointdeviceday.response.ResponseAppointDeviceDay;
import com.iot.inter.interfaceprocessing.newinterface.TurnTrafficUsageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 去北京做：3.6	查询指定设备一定时间范围内的流量使用情况:2.0接口
 * <p>
 * 接入方调用该接口，可以按天查询指定设备在一定时间范围内每天的流量使用情况
 * <p>
 * 【备注】
 * 	要求start_day必须晚于或者等于end_day;
 * 	如果查询某一天的流量使用情况，start_day和end_day取值一样；
 * 	end_day必须早于当天日期，即当天使用流量因为尚未统计完毕，不支持查询；
 * 	仅允许查询不超过31天的日流量使用情况；
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.6 查询指定设备一定时间范围内的流量使用情况")
public class TrafficUsageAppointDeviceDayController {

    private static Logger logger = Logger.getLogger(TrafficUsageAppointDeviceDayController.class);

    @CrossOrigin
    @RequestMapping(value = "appointDeviceDayTrafficUsage", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.6 查询指定设备一定时间范围内的流量使用情况", response = ResponseAppointDeviceDay.class)
    public String appointDeviceDayTrafficUsage(@ApiParam @RequestBody RequestAppointDeviceDay request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {

            logger.info("【查询指定设备一定时间范围内的流量使用情况】请求：" + requestStr);
            RequestAppointDeviceDay requestPackage = JSONArray.parseObject(requestStr, RequestAppointDeviceDay.class);

            String startDay = requestPackage.getStartDay();
            String endDay = requestPackage.getEndDay();
            String deviceId = requestPackage.getDeviceId();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            TurnTrafficUsageService service = new TurnTrafficUsageService();
            QuerDeviceDayFlowResponse response = service.appointDeviceDayTrafficUsage(deviceId, startDay, endDay, partnerCode, secret);

            if (response != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getRetCode())) {

                    List<Flow_by_day> fList = new ArrayList<Flow_by_day>();

                    if (response.getBody().getFlowByDay() != null) {
                        for (FlowByDay fd : response.getBody().getFlowByDay()) {

                            Flow_by_day f = new Flow_by_day();

                            f.setDay(fd.getDay());
                            f.setFlow(fd.getFlow());
                            fList.add(f);

                        }
                    }

                    ResponseAppointDeviceDay res = new ResponseAppointDeviceDay();
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    res.setFlow_by_day(fList);
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【查询指定设备一定时间范围内的流量使用情况】成功响应：" + jsonString);
                    return jsonString;

                } else {
                    ResponseAppointDeviceDay res = new ResponseAppointDeviceDay();
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【查询指定设备一定时间范围内的流量使用情况】响应：" + jsonString);
                    return jsonString;
                }

            } else {

                ResponseAppointDeviceDay res = new ResponseAppointDeviceDay();
                res.setRetCode("9998");
                res.setResultInfo("响应为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【查询指定设备一定时间范围内的流量使用情况】响应：" + jsonString);
                return jsonString;
            }

        } else {

            ResponseAppointDeviceDay response = new ResponseAppointDeviceDay();
            response.setRetCode("9998");
            response.setResultInfo("请求为空");
            jsonString = JSON.toJSONString(response);
            logger.info("【查询指定设备一定时间范围内的流量使用情况】响应：" + jsonString);
            return jsonString;


        }
    }
}